﻿@using Orchard.ContentManagement;
@using PJS.Bootstrap.Models;

@functions {
    // To support the layout classifaction below. Implementing as a razor function because we can, could otherwise be a Func<string[], string, string> in the code block following.
    string CalcuClassify(string[] zoneNames, string classNamePrefix) {
        var zoneCounter = 0;
        var zoneNumsFilled = string.Join("", zoneNames.Select(zoneName => { ++zoneCounter; return Model[zoneName] != null ? zoneCounter.ToString() : "";}).ToArray());
        return HasText(zoneNumsFilled) ? classNamePrefix + zoneNumsFilled : "";
    }
}
@{
    Display.Insertions();

    var theme = WorkContext.CurrentTheme;

    var settings = WorkContext.CurrentSite.As<ThemeSettingsPart>();

    /* Some useful shortcuts or settings
    ***************************************************************/

    Func<dynamic, dynamic> Zone = x => Display(x); // Zone as an alias for Display to help make it obvious when we're displaying zones

    /* Layout classification based on filled zones
    ***************************************************************/

    //Add classes to the wrapper div to toggle aside widget zones on and off
    var asideClass = CalcuClassify(new [] {"AsideFirst", "AsideSecond"}, "aside-"); // for aside-1, aside-2 or aside-12 if any of the aside zones are filled
    if (HasText(asideClass)) {
        Model.Classes.Add(asideClass);
    }

    //Add classes to the wrapper div to toggle tripel widget zones on and off
    var tripelClass = CalcuClassify(new [] {"TripelFirst", "TripelSecond", "TripelThird"}, "tripel-"); // for tripel-1, triple-2, etc. if any of the tripel zones are filled
    if (HasText(tripelClass)) {
        Model.Classes.Add(tripelClass);
    }

    //Add classes to the wrapper div to toggle quad widget zones on and off
    var footerQuadClass = CalcuClassify(new [] {"FooterQuadFirst", "FooterQuadSecond", "FooterQuadThird", "FooterQuadFourth"}, "split-"); // for quad-1, quad-2, etc. if any of the quad zones are filled
    if (HasText(footerQuadClass)) {
        Model.Classes.Add(footerQuadClass);
    }

    /* Last bit of code to prep the layout wrapper
    ***************************************************************/
    
    Model.Id = "layout-wrapper";
    var tag = Tag(Model, "div"); // using Tag so the layout div gets the classes, id and other attributes added to the Model
}
@tag.StartElement
@if (Model.Header != null) {
    <header id="layout-header" class="group">
        <div id="header">
            @Zone(Model.Header)
        </div>
    </header>
}
@if (Model.Navigation != null) {
    <div class="navbar-wrapper">
    <div class="navbar @(settings.UseInverseNav ? "navbar-inverse" : "navbar-default") @(settings.UseFixedNav ? "navbar-fixed-top" : string.Empty)">
        <div id="layout-navigation" class="group">
            @Zone(Model.Navigation)
        </div>
    </div>
    </div>
}

<section id="spalayout">
    @if (Model.Featured != null)
    {
        <div id="layout-featured" class="group">
            @Zone(Model.Featured)
        </div>
    }
    @if (Model.BeforeMain != null)
    {
        <div id="layout-before-main" class="group">
            @Zone(Model.BeforeMain)
        </div>
    }
    <div id="layout-main-container" style="padding-top:100px">
        <div id="layout-main" class="group">
            @if (Model.AsideFirst != null)
            {
                <aside id="aside-first" class="aside-first group">
                    @Zone(Model.AsideFirst)
                </aside>
            }
            <div id="layout-content" class="group">
                @if (Model.Messages != null)
                {
                    <div id="messages">
                        @Zone(Model.Messages)
                    </div>
                }
                @if (Model.BeforeContent != null)
                {
                    <div id="before-content">
                        @Zone(Model.BeforeContent)
                    </div>
                }
                @* the model content for the page is in the Content zone @ the default position (nothing, zero, zilch) *@
    @if (Model.Content != null)
    {
        <div id="content" class="group">
            @Zone(Model.Content)
        </div>
    }
                @if (Model.AfterContent != null)
                {
                    <div id="after-content">
                        @Zone(Model.AfterContent)
                    </div>
                }
            </div>
            @if (Model.AsideSecond != null)
            {
                <aside id="aside-second" class="aside-second">
                    @Zone(Model.AsideSecond)
                </aside>
            }
        </div>
    </div>
    @if (Model.AfterMain != null)
    {
        <div id="layout-after-main" class="group">
            @Zone(Model.AfterMain)
        </div>
    }
    @if (Model.TripelFirst != null || Model.TripelSecond != null || Model.TripelThird != null)
    {
        <div id="layout-tripel-container">
            <div id="layout-tripel" class="group">
                @* as in beer *@
                @if (Model.TripelFirst != null)
                {
                    <div id="tripel-first">
                        @Zone(Model.TripelFirst)
                    </div>
                }
                @if (Model.TripelSecond != null)
                {
                    <div id="tripel-second">
                        @Zone(Model.TripelSecond)
                    </div>
                }
                @if (Model.TripelThird != null)
                {
                    <div id="tripel-third">
                        @Zone(Model.TripelThird)
                    </div>
                }
            </div>
        </div>
    }

    @tag.EndElement

    @if (Model.Footer != null || Model.FooterQuadFirst != null || Model.FooterQuadSecond != null || Model.FooterQuadThird != null || Model.FooterQuadFourth != null)
    {
        <div id="layout-footer" class="group">
            <hr />
            <footer id="footer">
                @if (Model.FooterQuadFirst != null || Model.FooterQuadSecond != null || Model.FooterQuadThird != null || Model.FooterQuadFourth != null)
                {
                    <div id="footer-quad" class="group">
                        @if (Model.FooterQuadFirst != null)
                        {
                            <div id="footer-quad-first">
                                @Zone(Model.FooterQuadFirst)
                            </div>
                        }
                        @if (Model.FooterQuadSecond != null)
                        {
                            <div id="footer-quad-second">
                                @Zone(Model.FooterQuadSecond)
                            </div>
                        }
                        @if (Model.FooterQuadThird != null)
                        {
                            <div id="footer-quad-third">
                                @Zone(Model.FooterQuadThird)
                            </div>
                        }
                        @if (Model.FooterQuadFourth != null)
                        {
                            <div id="footer-quad-fourth">
                                @Zone(Model.FooterQuadFourth)
                            </div>
                        }
                    </div>
                }
                @if (Model.Footer != null)
                {
                    <div id="footer-sig" class="group">
                        @Zone(Model.Footer)
                    </div>
                }
            </footer>
        </div>
    }
    <div id="toTop">
        <h1><i class="fa fa-chevron-up"></i></h1>
    </div>
</section>

@if (settings.UseAsSpa) {
    <script type="text/javascript" src="@Url.Content(theme.Location + "/" + theme.Path + "/Scripts/jquery-ui-1.10.4.js")">

    </script>
    <script type="text/javascript" src="@Url.Content(theme.Location + "/" + theme.Path + "/Scripts/sammy-0.7.4.js")">

    </script>
    <script type="text/javascript" src="@Url.Content(theme.Location + "/" + theme.Path + "/Scripts/routing.js")">

    </script>
    <script>
        $(function() {
            var routing = new Routing('@Url.Content("~/") + @WorkContext.CurrentCulture.ToLower()', '#spalayout', '#/');

            routing.init();
        });
    </script>
}